import TreeNode from "../common/tree_node";
import { n1 } from "../common/tree_node";


function maxDepth_recur_shendu(root: TreeNode | null): number {
    if(root == null) return 0//终止条件
    let leftMax = maxDepth_recur_shendu(root.left)//子节点最大值
    let rightMax = maxDepth_recur_shendu(root.right)
    return Math.max(leftMax,rightMax) + 1//加上自身
};

//思路:queue装这一层所有的节点，只要这一层有节点，就+1，然后queue清空读下一层。
function maxDepth_guangdu(root: TreeNode | null): number {
  if(root == null) return 0
  let queue:TreeNode[] = [root]
  let ans = 0
  while(queue.length) {
    let curLevelCount = queue.length
    while(curLevelCount > 0) {//清空这一层，并读取下一层所有的
      let node = queue.shift()
      if(node?.left) {
        queue.push(node.left)
      }
      if(node?.right) {
        queue.push(node.right)
      }
      curLevelCount--

    }
    ans++
  }
  return ans
}
console.log(maxDepth_recur_shendu(n1))